﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.UI;

namespace AjaxSnippets
{
    /// <summary>
    /// Used in a SnippetCallback to define a mapping between a service method argument
    /// and a client dom element such as an input field. The mapping also defines the name of a 
    /// client function that will be used to extract a value from a DOM element to be used as 
    /// the service method argument value
    /// </summary>
    public class CallbackParameterMapping
    {
        /// <summary>
        /// The argument name of the web service callback function
        /// that will have its value set with the value that is extraced
        /// from the server control markup
        /// </summary>
        public string ArgumentName { get; set; }

        /// <summary>
        /// The ControlID of a server control that will have its value
        /// mapped to the service method argument, use ClientID if your
        /// not mapping to a server control
        /// </summary>
        public string ControlID { get; set; }
        /// <summary>
        /// The ClientID of the rendered server controls markup, use
        /// ControlID if mapping to a server control
        /// </summary>
        public string ClientID { get; set; }

        private string _ClientValueExtractorFunctionName;
        /// <summary>
        /// A function on the client that extracts the value
        /// from the server control markup, the functions are defined in the client
        /// class AjaxSnippets.ValueExtractors, the prototype can be extended to
        /// support custom extractors
        /// 
        /// If a ControlID is provided then the will automatically be determined from
        /// defaults or configuration
        /// </summary>
        public string ClientValueExtractorFunctionName 
        {
            get
            {
                // Ensure we have one by returning the default if one has
                // not been set
                if(String.IsNullOrEmpty(_ClientValueExtractorFunctionName))
                    return Config.Mappings["Default"];

                return _ClientValueExtractorFunctionName;
            }
            set
            {
                _ClientValueExtractorFunctionName = value;
            }
        }
 
        public override string ToString()
        {
            // param, dom id, extractor function
            return "{" + String.Format("'param':'{0}','id':'{1}','func':'{2}'", ArgumentName, ClientID, ClientValueExtractorFunctionName) + "}";
        }
    }
}
